package com.duoketech.controller;

import com.duoketech.model.member.Member;
import com.duoketech.utils.DbUtil;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.redis.Redis;

import java.util.HashMap;
import java.util.Map;

public abstract class BaseController extends Controller {

    public static final int COOKIE_TIME_OUT_1_MONTH=60*60*24*30;

    public static final String COOKIE_NAME="memberId";
    public static final String COOKIE_SHOP_NAME="memberId-shop";
    public int getMemberId(){
        Member member = getMember();
        return member.getInt(Member.ID);
    }

    public int getShopMemberId(){
        Member member = getShopMember();
        return member.getInt(Member.ID);
    }



    public String viewModel(){
        String result = this.getClass().getSimpleName();
        if(result.contains("Controller")){
            result = result.replace("Controller","");
        }else{
            result="view";
        }
        return result.toLowerCase();
    }


    public void listAlll(){

    }

    public void index(){
        render(viewModel()+"-list.html");
    }

    public void add(){

        render("add-"+viewModel()+".html");
    }


    public String travelView(String view){

        return "travel/"+view;
    }



    public void renderSuccess(String msg){
        Ret ret = Ret.create();
        ret.set("result",true);
        ret.set("data",msg);
        renderJson(ret);
    }

    public void renderError(String msg){
        Ret ret = Ret.create();
        ret.set("result",false);
        ret.set("data",msg);
        renderJson(ret);
    }

    public void renderObject(Object obj){
        Ret ret = Ret.create();
        ret.set("result",true);
        ret.set("data",obj);
        renderJson(ret);
    }


    public void renderKindEditorError(String message) {

        renderKindEditor(1, message);
    }

    public void renderKindEditorSuccess(String message) {

        renderKindEditor(0, message);
    }

    public void renderKindEditor(int result, String message) {
        Map<String, Object> json = new HashMap<String, Object>();
        json.put("error", result);
        json.put("url", message);
        renderJson(json);
    }

    public Member getShopMember(){
        Member member = DbUtil.getShopMember(getCookieToInt(COOKIE_SHOP_NAME,COOKIE_TIME_OUT_1_MONTH));
        return member;
    }

    public Member getMember(){
        Member member = DbUtil.getMember(getCookieToInt(COOKIE_NAME,COOKIE_TIME_OUT_1_MONTH));
        return member;
    }

    public void setMember(Member member){
        setCookie(COOKIE_NAME,member.getInt(Member.ID)+"",COOKIE_TIME_OUT_1_MONTH);
        DbUtil.setMember(member);
    }

    public void setMemberShop(Member member){
        setCookie(COOKIE_SHOP_NAME,member.getInt(Member.ID)+"",COOKIE_TIME_OUT_1_MONTH);
        DbUtil.setMemberShop(member);
    }

    public void removerMember(){
        DbUtil.removeMember(getCookieToInt(COOKIE_NAME,COOKIE_TIME_OUT_1_MONTH));
        removeCookie(COOKIE_NAME);
    }

    public void removerMemberShop(){
        DbUtil.removeMemberShop(getCookieToInt(COOKIE_SHOP_NAME,COOKIE_TIME_OUT_1_MONTH));
        removeCookie(COOKIE_SHOP_NAME);
    }
}
